﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ZCustomControl.Controls
{
    public class PathButton : Button
    {
        #region Dependency Property

        public static readonly DependencyProperty IconContentMarginProperty =
            DependencyProperty.Register("IconContentMargin", typeof(Thickness), typeof(PathButton), new PropertyMetadata(new Thickness(0, 0, 0, 0), null));
        public static readonly DependencyProperty GeometryProperty =
            DependencyProperty.Register("Geometry", typeof(Geometry), typeof(PathButton), new UIPropertyMetadata(Geometry.Empty));
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(PathButton), null);
        public static readonly DependencyProperty MouseOverBackgroundProperty =
            DependencyProperty.Register("MouseOverBackground", typeof(Brush), typeof(PathButton));
        public static readonly DependencyProperty MouseDownBackgroundProperty =
            DependencyProperty.Register("MouseDownBackground", typeof(Brush), typeof(PathButton));
        public static readonly DependencyProperty PathFillProperty =
            DependencyProperty.Register("PathFill", typeof(Brush), typeof(PathButton), new PropertyMetadata(new SolidColorBrush(Colors.Gray)));
        public static readonly DependencyProperty PathMouseOverFillProperty =
            DependencyProperty.Register("PathMouseOverFill", typeof(Brush), typeof(PathButton), null);
        public static readonly DependencyProperty PathMouseDownFillProperty =
            DependencyProperty.Register("PathMouseDownFill", typeof(Brush), typeof(PathButton), null);
        public static readonly DependencyProperty PathHeightProperty =
            DependencyProperty.Register("PathHeight", typeof(double), typeof(PathButton), new PropertyMetadata(24.0, null));
        public static readonly DependencyProperty PathWidthProperty =
            DependencyProperty.Register("PathWidth", typeof(double), typeof(PathButton), new PropertyMetadata(24.0, null));

        #endregion

        #region Property Wrappers

        public Thickness IconContentMargin
        {
            get { return (Thickness)GetValue(IconContentMarginProperty); }
            set { SetValue(IconContentMarginProperty, value); }
        }

        /// <summary>
        /// Path.Data
        /// </summary>
        public Geometry Geometry
        {
            get { return (Geometry)GetValue(GeometryProperty); }
            set { SetValue(GeometryProperty, value); }
        }

        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        public Brush MouseOverBackground
        {
            get
            {
                if ((Brush)GetValue(MouseOverBackgroundProperty) == null)
                {
                    var col = (Brush)GetValue(BackgroundProperty);
                    MouseOverBackground = col;
                    return col;
                }
                else
                    return (Brush)GetValue(MouseOverBackgroundProperty);
            }
            set { SetValue(MouseOverBackgroundProperty, value); }
        }

        public Brush MouseDownBackground
        {
            get
            {
                if ((Brush)GetValue(MouseDownBackgroundProperty) == null)
                {
                    var col = (Brush)GetValue(MouseOverBackgroundProperty);
                    MouseDownBackground = col;
                    return col;
                }
                else
                    return (Brush)GetValue(MouseDownBackgroundProperty);
            }
            set { SetValue(MouseDownBackgroundProperty, value); }
        }

        public Brush PathFill
        {
            get { return (Brush)GetValue(PathFillProperty); }
            set { SetValue(PathFillProperty, value); }
        }

        public Brush PathMouseOverFill
        {
            get
            {
                if ((Brush)GetValue(PathMouseOverFillProperty) == null)
                {
                    var col = (Brush)GetValue(PathFillProperty);
                    PathMouseOverFill = col;
                    return col;
                }
                else
                    return (Brush)GetValue(PathMouseOverFillProperty);
            }
            set { SetValue(PathMouseOverFillProperty, value); }
        }

        public Brush PathMouseDownFill
        {
            get
            {
                if ((Brush)GetValue(PathMouseDownFillProperty) == null)
                {
                    Brush color = null;
                    if ((Brush)GetValue(PathMouseOverFillProperty) != null)
                    {
                        color = (Brush)GetValue(PathMouseOverFillProperty);
                        PathMouseDownFill = color;
                    }
                    else
                    {
                        color = (Brush)GetValue(PathFillProperty);
                        PathMouseDownFill = color;
                    }
                    return color;
                }
                else
                    return (Brush)GetValue(PathMouseDownFillProperty);

            }
            set { SetValue(PathMouseDownFillProperty, value); }
        }

        public double PathHeight
        {
            get { return (double)GetValue(PathHeightProperty); }
            set { SetValue(PathHeightProperty, value); }
        }

        public double PathWidth
        {
            get { return (double)GetValue(PathWidthProperty); }
            set { SetValue(PathWidthProperty, value); }
        }

        #endregion

        #region Constructors

        static PathButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PathButton), new FrameworkPropertyMetadata(typeof(PathButton)));
        }

        #endregion

        #region Override Methods

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            if (this.PathMouseOverFill == null)
            {
                this.PathMouseOverFill = PathFill;
            }
            if (this.PathMouseDownFill == null)
            {
                if (this.PathMouseOverFill != null)
                {
                    this.PathMouseOverFill = PathMouseOverFill;
                }
                else
                {
                    this.PathMouseOverFill = PathFill;
                }
            }
            if (this.MouseOverBackground == null)
            {
                this.MouseOverBackground = Background;
            }
            if (this.MouseDownBackground == null)
            {
                if (this.MouseOverBackground != null)
                {
                    this.MouseOverBackground = MouseOverBackground;
                }
                else
                {
                    this.MouseOverBackground = MouseOverBackground;
                }
            }
        }

        #endregion
    }
}
